import socket
import datetime
import json

ip = '10.11.140.210'
port = 9999
# 1.创建tcp端套接字
socket_client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
# 2.和服务端建立连接
socket_client.connect((ip, port))

while True:
    # 获取当前系统的时间
    dict1={}
    time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    send_content = input(f"客户端:{time}:")
    dict1["姓名"]="服务器"
    dict1["时间"] = time
    dict1["说"] = send_content
    send_content = json.dumps(dict1)
    # 对字符串编码成为二进制数据
    send_data = send_content.encode("utf-8")
    # 3.发送数据到服务端
    socket_client.send(send_data)
    # 4.接收服务端的数据
    # 1024表示每次接收的最大字节
    recv_data =  socket_client.recv(1024)
    recv_data = recv_data.decode("utf-8")
    recv_data = json.loads(recv_data)
    print("接收到的服务器的数据为", recv_data)
# 5.关闭套接字
socket_client.close()